/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gr.anagnosg.image.imagemagick;

import gr.anagnosg.general.utils.Parser;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.im4java.core.IMOperation;


/**
 *
 * @author anagnosg
 */
public class ParamParser {
    //String parameter = "";
    
    public static int STATUS_START =0;
    public static int STATUS_RESIZE_START=1;
    public static int STATUS_GEOMETRY_INTEGER_FOUND=2;
    public static int STATUS_GEOMETRY_HEIGHT_FOUND=3;
    public static int STATUS_GEOMETRY_WEIGHT_FOUND=4;
    public static int STATUS_RESIZE_FINISH=5;
    
    public static int STATUS_RESIZE_FINISH_PERCENT=6;
    public static int STATUS_RESIZE_FINISH_WIDTH_HEIGHT=7;
    public static int STATUS_RESIZE_FINISH_WIDTH_HEIGHT_CHAR=8;
    public static int STATUS_RESIZE_FINISH_WIDTH_HEIGHT_STRING=9;
    
    
    public static int STATUS_THUMBNAIL_START=20;
    
    
    
    
    
    
    public static Param parseParameters(String parameters, String action ) throws ParseParamsException{
        Param output = new Param();
        boolean run = true;
        int status = 0 ;
        
        if(action.equals(Constants.RESIZE_STR)){
            status = ParamParser.STATUS_RESIZE_START;
            output.setType(action);
        }
        Parser p = new Parser(parameters);
        List pVList = new ArrayList();
        
        while (run){
            //RESIZE_STR
            //get next element
            ParamElement par = p.getNextParam();
            /*if(par==null){
                break;
            }*/
            if(ParamParser.STATUS_RESIZE_START == status){
                if(par.getType()==ParamElement.TYPE_NUM){
                    par.setDisplayOnly(false);
                    pVList.add(par);
                    status = STATUS_GEOMETRY_INTEGER_FOUND;
                }
                //other cases. 
                
            }
            else if(status == STATUS_GEOMETRY_INTEGER_FOUND){// an integer param found.
                if(par.getType()==ParamElement.TYPE_SPECIAL_CHAR && par.getValue().equals("x")){
                    //previous param was the height
                    //no add.
                    status = STATUS_GEOMETRY_HEIGHT_FOUND;
                    par.setDisplayOnly(true);
                    pVList.add(par);
                }
                else if(par.getType()==ParamElement.TYPE_SPECIAL_CHAR && par.getValue().equals("%")){
                    //status percentange scale 
                    par.setDisplayOnly(true);
                    pVList.add(par);
                    //finish
                    output.setElList(pVList);
                    output.setType(Constants.RESIZE_STR);
                    output.setFinishStatus(STATUS_RESIZE_FINISH_PERCENT);
                    run=false;
                }
                else {
                    
                }
            }
            else if(status == STATUS_GEOMETRY_HEIGHT_FOUND){
                if(par.getType()==ParamElement.TYPE_NUM){//weight param.
                    pVList.add(par);
                    status = STATUS_GEOMETRY_WEIGHT_FOUND;
                }
                else{
                    String exmessage = ParseParamsException.exMessage("STATUS_RESIZE_HEIGHT_FOUND", "TYPE_NUM",par.getValue());
                    throw new ParseParamsException(exmessage);
                }
            
            }
            else if(status == STATUS_GEOMETRY_WEIGHT_FOUND){
                if(par==null){//no other parameter element finish.
                    status =STATUS_RESIZE_FINISH;
                    output.setFinishStatus(STATUS_RESIZE_FINISH_WIDTH_HEIGHT);
                    // IMOperation imoper = new IMOperation ();
                    //create the operation object.
                    //imoper.resize((Integer)pVList.get(0), (Integer)pVList.get(1), (((String)par.getValue()).charAt(0)));
                }
                else if(par.getType()==ParamElement.TYPE_SPECIAL_CHAR  ){
                    par.setDisplayOnly(false);
                    pVList.add(par);
                    status =STATUS_RESIZE_FINISH;
                    output.setFinishStatus(ParamParser.STATUS_RESIZE_FINISH_WIDTH_HEIGHT_CHAR);
                    //create the operation object.
                }
                else if( par.getType()==ParamElement.TYPE_SPECIAL_STRING){
                    par.setDisplayOnly(false);
                    pVList.add(par);
                    status =STATUS_RESIZE_FINISH;
                     output.setFinishStatus(ParamParser.STATUS_RESIZE_FINISH_WIDTH_HEIGHT_STRING);
                }
                output.setElList(pVList);
                output.setType(Constants.RESIZE_STR);
               
                run = false;
            }
            
            
        }
        return output;
    }
    
    
}
